/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.MerchantTypeEnum;
import com.ykht.proj.coupon.entity.FxConfBrandPerson;
import com.ykht.proj.coupon.mapper.FxMerchantAccountMapper;
import com.ykht.proj.coupon.service.FxConfBrandPersonService;
import com.ykht.proj.coupon.vo.FxConfBrandPersonVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;


/**
 * 品牌对c端的分销表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfbrandperson" )
@Api(value = "fxconfbrandperson", tags = "品牌对c端的分销表管理")
@Slf4j
public class FxConfBrandPersonController {

    private final  FxConfBrandPersonService fxConfBrandPersonService;

    /**
     * 分页查询 品牌对c端的分销表
     * @param page 分页对象
     * @param fxConfBrandPersonVo 品牌对c端的分销表
     * @return
     */
    @ApiOperation(value = "分页查询 品牌对c端的分销表", notes = "分页查询 品牌对c端的分销表")
    @GetMapping("/page" )
    public R<IPage<FxConfBrandPersonVo>> getFxConfBrandPersonPage(Page page, FxConfBrandPersonVo fxConfBrandPersonVo) {
		log.info("->分页查询 品牌对c端的分销表 :{}", page, fxConfBrandPersonVo);
    	IPage<FxConfBrandPersonVo> iPage = fxConfBrandPersonService.getFxConfBrandPersonPage(page, fxConfBrandPersonVo);
        return R.ok(iPage);
    }


    /**
     * 通过id查询品牌对c端的分销表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R<FxConfBrandPersonVo> getById(@PathVariable("id" ) Long id) {
		log.info("->通过id查询品牌对c端的分销表 :{}", id);
		FxConfBrandPersonVo fxConfBrandPersonVo = fxConfBrandPersonService.getByIdFxConfBrandPerson(id);
        return R.ok(fxConfBrandPersonVo);
    }

	@ApiOperation(value = "获取详情配置", notes = "分页查询 品牌对c端的分销表")
	@GetMapping("/getOne")
	public R<FxConfBrandPerson> getone() {
		log.info("->根据登陆商家获取配置 :{}", SecurityUtils.getSjId());
		return R.ok(fxConfBrandPersonService.getOne(Wrappers.lambdaQuery(FxConfBrandPerson.class).eq(FxConfBrandPerson::getBrandId,SecurityUtils.getSjId())));
	}



	/**
	 * 新增或者新增 品牌对c端的分销表
	 * @param fxConfBrandPerson 品牌对c端的分销表
	 * @return R
	 */
//	@ApiOperation(value = "新增或者新增 品牌对c端的分销表", notes = "新增或者新增 品牌对c端的分销表")
//	@SysLog("新增或者新增 品牌对c端的分销表" )
//	@PostMapping("addOrUpdateConfBrandPerson")
//	public R addOrUpdateConfBrandPerson(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
//		log.info("->新增或者新增 品牌对c端的分销表 :{}", fxConfBrandPerson);
//		return R.ok(fxConfBrandPersonService.addOrUpdateConfBrandPerson(fxConfBrandPerson));
//	}
	@ApiOperation(value = "新增或者新增 品牌对c端的分销表", notes = "新增或者新增 品牌对c端的分销表")
	@SysLog("新增或者新增 品牌对c端的分销表" )
	@PostMapping("create")
	public R create(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		log.info("->新增 品牌对c端的分销表 :{}", fxConfBrandPerson);
		return R.ok(fxConfBrandPersonService.create(fxConfBrandPerson));
	}
	@ApiOperation(value = "更新品牌对c端的分销表")
	@SysLog("更新品牌对c端的分销表" )
	@PostMapping("update")
	public R update(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		log.info("->更新品牌对c端的分销表 :{}", fxConfBrandPerson);
		if(fxConfBrandPerson.getId()==null) return R.failed("id为空");
		return R.ok(fxConfBrandPersonService.updateObj(fxConfBrandPerson));
	}
	@ApiOperation(value = "结束品牌对c端的分销表")
	@SysLog("结束品牌对c端的分销表" )
	@PostMapping("finish")
	public R finish(@Validated @NotNull Long id) {
		log.info("->结束品牌对分销配置 :{}", id);
		if(id == null) return R.failed("id为空");
		return R.ok(fxConfBrandPersonService.finish(id));
	}

//	@Autowired
//	private FxMerchantAccountMapper fxMerchantAccountMapper;
//
//	@Inner(false)
//	@GetMapping("qqq")
//	public R qqqq() {
//		fxMerchantAccountMapper.updateMerchantAccount4ConfFx(MerchantTypeEnum.brand.getValue(), 137L, BigDecimal.valueOf(100), 11L);
//		return R.ok();
//	}


	/**
     * 新增品牌对c端的分销表
     * @param fxConfBrandPerson 品牌对c端的分销表
     * @return R
     */
    @ApiOperation(value = "新增品牌对c端的分销表", notes = "新增品牌对c端的分销表")
    @SysLog("新增品牌对c端的分销表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_add')" )
    public R save(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
        return R.ok(fxConfBrandPersonService.save(fxConfBrandPerson));
    }

    /**
     * 修改品牌对c端的分销表
     * @param fxConfBrandPerson 品牌对c端的分销表
     * @return R
     */
    @ApiOperation(value = "修改品牌对c端的分销表", notes = "修改品牌对c端的分销表")
    @SysLog("修改品牌对c端的分销表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_edit')" )
    public R updateById(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
        return R.ok(fxConfBrandPersonService.updateById(fxConfBrandPerson));
    }

    /**
     * 通过id删除品牌对c端的分销表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除品牌对c端的分销表", notes = "通过id删除品牌对c端的分销表")
    @SysLog("通过id删除品牌对c端的分销表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_del')" )
    public R removeById(@PathVariable Long id) {
		FxConfBrandPerson byId = fxConfBrandPersonService.getById(id);
		if (byId.getSwitchs()==2){
			return R.ok(fxConfBrandPersonService.removeById(id));
		}
		return R.failed("当前配置不可被删除");
	}

}
